package com.demo2do.cda.admin.web.controller.system;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.cda.admin.web.interceptor.MenuSetting;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;

/**
 * this bit is copy and pasted. commenting this out does not make any difference.
 * because principals was mapped in the home controller
 * 
 * @author Sanz
 *
 */
@Controller
@RequestMapping("/principals")
@MenuSetting("menu-principal")
public class PrincipalController {
    
    /**
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "" ,  method = RequestMethod.GET)
    public ModelAndView list(Page page) {
        
        ModelAndView modelAndView = new ModelAndView("system/principal/principal-list");
        
        int totals = 100;
        page.with(totals);
        page.calculate();
        
        modelAndView.addObject("totals", totals);
        
        return modelAndView;
    }
    
    /**
     * search
     * 
     * @return
     */
    @RequestMapping(value = "/search",  method = RequestMethod.GET)
    public ModelAndView search() {
        
        ModelAndView modelAndView = new ModelAndView("system/principal/principal-search");
        
        return modelAndView;
    }
       
    /**
     * onSearch
     * 
     * @return
     */
    @RequestMapping(value = "",  method = RequestMethod.GET, params = "filter")
    public ModelAndView onSearch() {
        
        ModelAndView modelAndView = new ModelAndView("system/principal/principal-list");
        
        return modelAndView;
    }
    
    /**
     * view(user detail)
     * 
     * @param principalId
     * @return
     */
    @RequestMapping(value = "/{principalId}" ,method = RequestMethod.GET)
    public ModelAndView view(@PathVariable("principalId") int principalId){
        
        ModelAndView modelAndView = new ModelAndView("system/principal/principal-view");
        // Impractical, only use for loop for the test

        return modelAndView;
    }
    
    /**
     * create
     * 
     * @return
     */
    @RequestMapping(value = "/create",method = RequestMethod.GET)
    public ModelAndView create() {
        
        ModelAndView modelAndView = new ModelAndView("system/principal/principal-create");
        
        return modelAndView;
    }
    
    /**
     * onCreate
     * 
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.POST)
    public @ResponseBody String onCreate(){
        
        HttpResult httpResult = new HttpResult();
        
        httpResult.success().message("新增员工成功");
        
        return JsonUtils.toJson(httpResult);
    }
    

    /**
     * edit
     * 
     * @return
     */
    @RequestMapping(value = "/{principalId}/edit", method = RequestMethod.GET)
    public ModelAndView edit() {
        
        ModelAndView modelAndView = new ModelAndView("system/principal/principal-edit");
        
        return modelAndView;
    }
    
    /**
     * onEdit
     * 
     * @return
     */
    @RequestMapping(value = "/{principalId}" , method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(){
        return null;
    }
    
    /**
     * delete
     * 
     * @return
     */
    @RequestMapping(value = "/{principalId}/delete", method = RequestMethod.GET)
    public ModelAndView delete() {
        
        ModelAndView modelAndView = new ModelAndView("system/principal/principal-delete");
        
        return modelAndView;
    }
    
    /**
     * onDelete - not working
     * 
     * @param principalId
     * @return
     */
    @RequestMapping(value = "/{principalId}" , method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@PathVariable("principalId") int principalId){
        
        HttpResult httpResult = new HttpResult();

        try {

            httpResult.success().message("删除动作成功！");
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("删除动作失败！");
        } 
        
        return JsonUtils.toJson(httpResult);
    }
        
    
}


